﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExamSystem.BLL;
using ExamSystem.Mdeol;
namespace ExamSystem.ht
{
    public partial class TStudent_Edit : System.Web.UI.Page
    {
        private TclassBLL tb = new TclassBLL();
        private TstudentBLL tdb = new TstudentBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //获取id
                string tsid = Request.QueryString["id"];
                if (!string.IsNullOrEmpty(tsid))
                {
                    //查询的班级集合
                    List<Tclass> list = tb.selecttclass();
                    Tclass bi = new Tclass(0, "--请选择--");
                    list.Insert(0, bi);

                    //绑定显示的数据
                    this.tcId.DataSource = list;
                    this.tcId.DataTextField = "tcno";
                    this.tcId.DataValueField = "tcid";
                    this.tcId.DataBind();

                    //查询学生集合
                    Tstudent bf = tdb.selecttsid(int.Parse(tsid));
                    //遍历图书集合
                    if(bf!=null)
                    {
                        this.tcId.Value = bf.TcId.TcId + "";
                        string tssex = bf.TsSex;
                        if (tssex=="女")
                        {
                            this.sex2.Checked = true;
                        }
                        else
                        {
                            this.sex1.Checked = true;
                        }

                        this.tsNo.Value = bf.TsNo;
                        this.tsName.Value = bf.TsName;
                        this.tsPwd.Value = bf.TsPwd;
                        this.tsPhone.Value = bf.TsPhone;
                    }
                }

            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //获取文本显示的值
                string tcid = this.tcId.Value;
                if(string.IsNullOrEmpty(tcid))
                {
                    this.lbMsg.InnerText = "请选择班级";
                    return;
                }

                //班级类型
                Tclass tt = null;
                //判断图书编号是否等于零
                if (int.Parse(tcid) != 0)
                {
                    tcid=this.tcId.Value;
                }
                else
                {
                    util.FunHelper.LayerAlter(this, "id不合法");
                }

                string tsid = Request.QueryString["id"];
                if(string.IsNullOrEmpty(tsid))
                {
                    this.lbMsg.InnerText = "id非法";
                    return;
                }
                tt= new Tclass(int.Parse(tcid),"");
                string tsName = this.tsName.Value;
                string tssex = this.sex1.Value;
                if (sex2.Checked)
                {
                    tssex = this.sex2.Value;
                }
                string tsno = this.tsNo.Value;
                string tspwd = this.tsPwd.Value;
                string tsphone = this.tsPhone.Value;
                //验证
                if (string.IsNullOrEmpty(tsName))
                {
                    this.lbMsg.InnerText = "请输入学生姓名";
                    return;
                }
                if (string.IsNullOrEmpty(tssex))
                {
                    this.lbMsg.InnerText = "请选择性别";
                    return;
                }
                if (string.IsNullOrEmpty(tsno))
                {
                    this.lbMsg.InnerText = "请输入学号";
                    return;
                }
                else
                {
                    if (tdb.selecttsno(tsno) > 0)
                    {
                        this.lbMsg.InnerText = "学生学号重复";
                        return;
                    }
                }
                if (string.IsNullOrEmpty(tspwd))
                {
                    this.lbMsg.InnerText = "请输入密码";
                    return;
                }
                if (string.IsNullOrEmpty(tsphone))
                {
                    this.lbMsg.InnerText = "请输入电话";
                    return;
                }
                else
                {
                    if (tsphone.Length != 11)
                    {
                        this.lbMsg.InnerText = "电话号码为11位";
                        return;
                    }
                }
                //入库

                Tclass tk = new Tclass(int.Parse(tcid), null);
                Tstudent tq = new Tstudent(int.Parse(tsid),tk,tsno,tsName,tspwd,tssex,tsphone);
                int b = tdb.update(tq);
                if (b > 0)
                {
                    util.FunHelper.LayerAlter(this, "修改成功", "TStudent_List.aspx");
                }
                else
                {
                    util.FunHelper.LayerAlter(this, "修改失败");
                }
            }
            catch (Exception ex)
            {
                util.FunHelper.LayerAlter(this, ex + "");
            }
        }
    }
}